package com.tongji.seller.view.twoD
{
	
	import com.tongji.seller.utils.ConstVariableInventory;
	import com.tongji.seller.utils.TestImage;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.net.*;
	import flash.ui.Mouse;
	
	import mx.controls.Alert;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	/**
	 * 对图片进行预上传，从而获取其bitmapData，顺便进行缩放
	 * @author chenzhongyuan
	 * 2011-7-28
	 */

	public class AddBitmap extends UIComponent 
	{	
		private var loader:Loader = new Loader();
		private var pointX:Number;
		private var pointY:Number;
		public var addBitmapData:BitmapData;
		private var addBitmap:Bitmap;

		private var loading:TestImage = new TestImage();
		
		
		public function AddBitmap(X:Number, Y:Number, urls:String = null, bitmap:Bitmap = null):void
		{
			if (urls != null)
			{
				loader.load(new URLRequest(urls));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			}
			else if (bitmap != null)
			{
				addBitmap = bitmap;
				finalFunction();
			}
			else
			{
			}
			pointX = X;
			pointY = Y;			
		}
		
		protected function onProgressHandler(event:ProgressEvent):void
		{
			addChild(loading);
			loading.x = pointX;
			loading.y = pointY;
		}
		
		public function onComplete(e:Event):void
		{
			removeChild(loading);
			addBitmap = Bitmap(loader.content);
			finalFunction();
		}
		
		private function finalFunction():void
		{
			addBitmap.x =  ConstVariableInventory.X_SCALE * (pointX - addBitmap.width / 2);
			addBitmap.y =  ConstVariableInventory.Y_SCALE * (pointY - addBitmap.height / 2);	
			addBitmap.scaleX = ConstVariableInventory.X_SCALE;
			addBitmap.scaleY = ConstVariableInventory.Y_SCALE;	
			addBitmap.smoothing = true;
			addBitmapData = addBitmap.bitmapData;
			addChild(addBitmap);
		}
	}
}